//
//  LHHomeController.swift
//  sina_weibo
//
//  Created by 刘刘海强 on 16/5/11.
//  Copyright © 2016年 chuanzhi. All rights reserved.
//

import UIKit

let HomeTableViewCellIdentifier = "HomeTableViewCellIdentifier"

class LHHomeController: LHVisitorController {
    
    private lazy var statusListViewModel: LHStatusListViewModel = LHStatusListViewModel()
    
    //上拉加载视图
    private lazy var  pullUpView: UIActivityIndicatorView = {
        let view = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.WhiteLarge)
        view.color = UIColor.redColor()
        return view
    }()
    
    //系统提供下拉刷新视图
    private lazy var refreshCtr: UIRefreshControl = UIRefreshControl()
    
    //自定义下拉刷新视图
    private lazy var pullDownView: CZRefreshControl = CZRefreshControl()
    
    //tip动画控件
    private lazy var tipLabel: UILabel = {
        let label = UILabel(textColor: UIColor.whiteColor(), fontSize: 12)
        label.backgroundColor = UIColor.orangeColor()
        label.textAlignment = .Center
        label.text = "买法拉利，买宾利"
        label.alpha = 0
        return label
    }()

    override func viewDidLoad() {
        super.viewDidLoad()
       
        if userLogin {
            setupTableView()
            loadStatus()
            setupUI()
            
        }else{
            vistorView?.setVistorInfo(nil, imageName: nil)
        }
        
       
    }
    
    private func setupTableView() {
        tableView.registerClass(LHStatusTableViewCell.self, forCellReuseIdentifier: HomeTableViewCellIdentifier)
        
        //设置自动计算行高，预估行高时，必须把子控件添加到contentview上，不能添加到cell上，也就是不能添加到self上
        tableView.rowHeight = UITableViewAutomaticDimension
        tableView.estimatedRowHeight = 200
        
        tableView.separatorStyle = .None
        
        //添加上拉加载视图
        tableView.tableFooterView = pullUpView
        pullUpView.sizeToFit()
        
        //添加下拉刷新视图
//        refreshCtr.addTarget(self, action: "pullDownRefresh", forControlEvents: .ValueChanged)
//        tableView.addSubview(refreshCtr)
        
        //添加自定义下拉刷新视图
        pullDownView.addTarget(self, action: "pullDownRefresh", forControlEvents: .ValueChanged)
        tableView.addSubview(pullDownView)
        
    }
    
    
    @objc private func pullDownRefresh() {

        loadStatus()
        
    }
    
    
    //请求数据
    private func loadStatus() {
    
        statusListViewModel.requestStatus(pullUpView.isAnimating(), complete: { (message, isSuccess) -> () in
            
            if !self.pullUpView.isAnimating() {
                self.startTipAnimation(message)
            }
            
            //请求成功后停止刷新动画
            self.endRefreshing()
            
            if isSuccess {
                self.tableView.reloadData()
            }
        })
    }
    
    private func endRefreshing() {
        pullUpView.stopAnimating()
        refreshCtr.endRefreshing()
        
        //结束自定义下拉刷新
        pullDownView.endRefreshing()
    }
    
    private func setupUI() {
        //添加tipLabel
        if let nav = self.navigationController {
            tipLabel.frame = CGRectMake(0, CGRectGetMaxY(nav.navigationBar.frame), tableView.frame.size.width, 35)
            nav.view.addSubview(tipLabel)
        }
    }
    
    //tipLabel动画
    private func startTipAnimation(message: String) {
        
        //防止重复加载
        if tipLabel.alpha != 0 {
            return
        }
        
        tipLabel.text = message
        UIView.animateWithDuration(1, animations: { () -> Void in
            self.tipLabel.alpha = 1
            }) { (_) -> Void in
                UIView.animateWithDuration(1, animations: { () -> Void in
                    self.tipLabel.alpha = 0
                })
        }
        
    }
 
  
}


//实现数据源代理方法
extension LHHomeController {
    
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return statusListViewModel.statusList?.count ?? 0
    }
    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier(HomeTableViewCellIdentifier, forIndexPath: indexPath) as!LHStatusTableViewCell
        cell.statusViewModel = statusListViewModel.statusList![indexPath.row]
        
        cell.selectionStyle = .None
        return cell
    }
    
    
    //显示行号
    override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
//        print(indexPath.row)
        
        if indexPath.row == (statusListViewModel.statusList?.count)! - 1 && !pullUpView.isAnimating() {
           
            pullUpView.startAnimating()

            print("这是最后一条微博了")
            
            loadStatus()
        }
    }

}









